package com.wtwd.device.service.impl;

import com.wtwd.device.constant.RespCode;
import com.wtwd.device.constant.UploadFileEnum;
import com.wtwd.device.model.entity.ExcelImporthealthTypeLanguage;
import com.wtwd.device.model.vo.UploadFileVO;
import com.wtwd.device.service.FileService;
import com.wtwd.device.util.FileUtils;
import com.wtwd.device.utils.MyExcelUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Created by mjy on 2023/8/17 9:09
 */
@Service
public class FileServiceImpl implements FileService<ExcelImporthealthTypeLanguage> {


    @Value("${healthType.excel}")
    private String healthTypePath;

    @Value("${file.path}")
    private String filePath;

    @Value("${healthContent.excel}")
    private String healthContentPath;

    @Value("${healthType.images}")
    private String healthTypeImages;


    @Override
    public UploadFileVO uploadFile(MultipartFile file, String path) throws Exception {
        if (StringUtils.isBlank(path) || file == null) {
            throw new NullPointerException(RespCode.NO_PARAM.getMsg());
        }
        File saveFile = new File(path);
        if (!saveFile.exists()) {
            //文件不存在则创建一个
            saveFile.mkdir();
        }
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString().concat(originalFilename);
        FileUtils.uploadFile(path, fileName, file.getInputStream());
        UploadFileVO uploadFileVO = new UploadFileVO();
        uploadFileVO.setFileUrl(fileName);
        return uploadFileVO;
    }

    @Override
    public String getUploadFilePath(Integer type) {
        String path = "";
        switch (UploadFileEnum.getUploadFileEnum(type)) {
            case HEALTH_TYPE_INTERNATIONALIZED_DOCUMENT:
                path = filePath.concat(healthTypePath);
                break;
            case HEALTH_CONTENT_INTERNATIONALIZED_DOCUMENT:
                path = filePath.concat(healthContentPath);
                break;
            case HEALTH_TYPE_IMAGE:
                path = filePath.concat(healthTypeImages);
                break;
        }
        return path;
    }

    @Override
    public UploadFileVO uploadFile(MultipartFile file, Integer type) throws Exception {
        if (file == null || type == null) {
            return null;
        }
        return uploadFile(file, getUploadFilePath(type));
    }

    @Override
    public List<ExcelImporthealthTypeLanguage> readExcel(String filePath) throws Exception {
        MultipartFile file = getMultipartFile(filePath);
        List<ExcelImporthealthTypeLanguage> datas = MyExcelUtils.importExcel(file, 0, 1, ExcelImporthealthTypeLanguage.class);
        if (datas != null && datas.size() > 0) {
            datas = datas.stream().filter(e -> StringUtils.isNotBlank(e.getTranslation())||StringUtils.isNotBlank(e.getTitle())).collect(Collectors.toList());
        }
        return datas;
    }


    private MultipartFile getMultipartFile(String path) {
        File file = new File(path);
        FileItem item = new DiskFileItemFactory().createItem("file"
                , MediaType.MULTIPART_FORM_DATA_VALUE
                , true
                , file.getName());
        try (InputStream input = new FileInputStream(file);
             OutputStream os = item.getOutputStream()) {
            // 流转移
            IOUtils.copy(input, os);
        } catch (Exception e) {
            return null;
        }
        return new CommonsMultipartFile(item);
    }
}
